/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Entity.Dosen;
import Entity.PenawaranPpm;
import Model.DaftarPenawaranPpm;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author user
 */
public class DetailBeritaPenawaranPpmDosen extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String id_user = (String)session.getAttribute("id_user");

        ArrayList ListRole = new ArrayList();
        ListRole = (ArrayList) session.getAttribute("role");

        boolean bukanRole = true;
        if (session.getAttribute("role") != null) {
            for (int i = 0; i < ListRole.size(); i++) {
                if (ListRole.get(i).equals("dosen")) {
                    bukanRole = false;
                }
            }
        }

        if (session.getAttribute("role") == null) {
            Login servlet = new Login();
            servlet.processRequest(request, response);
        } else if (bukanRole) {
            request.setAttribute("menu", "home");
            request.setAttribute("submenu", "");
            RequestDispatcher dis = request.getRequestDispatcher("AccessDenied.jsp");
            dis.forward(request, response);
        } else {

            PenawaranPpm penawaranPpm = new PenawaranPpm();
            DaftarPenawaranPpm daftarPenawaranPpm = new DaftarPenawaranPpm();
            int noPenawaran = Integer.parseInt(request.getParameter("noPenawaran"));

            penawaranPpm.setIdPenawaran(noPenawaran);
            penawaranPpm = daftarPenawaranPpm.detailPenawaranPpm(penawaranPpm);
            
            List<Dosen> listDosenPendaftar = new ArrayList<Dosen>();
            listDosenPendaftar = daftarPenawaranPpm.daftarPendaftarPenawaranPpm(penawaranPpm);
            
            request.setAttribute("pendaftarPenawaran", listDosenPendaftar);
            String tampil = "true";
            String batasHabis = "false";
            
            for(int i = 0; i < listDosenPendaftar.size(); i++){
                if(listDosenPendaftar.get(i).getUser().getIdUser().equals(id_user)){
                    tampil = "false";
                    break;
                }
            }
            
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            int Month = c.get(Calendar.MONTH) + 1;
            Date tanggal = null;
            try {
                 tanggal = df.parse(c.get(Calendar.YEAR) + "-" + Month + "-" + c.get(Calendar.DATE));
            } catch (ParseException ex) {
                Logger.getLogger(DetailBeritaPenawaranPpmDosen.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                if(tanggal.after(df.parse(penawaranPpm.getBatasPendaftaran()))){
                    batasHabis = "true";
                }
            } catch (ParseException ex) {
                Logger.getLogger(DetailBeritaPenawaranPpmDosen.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            request.setAttribute("batasHabis", batasHabis);
            request.setAttribute("tampil", tampil);
            request.setAttribute("penawaranPpm", penawaranPpm);
            request.setAttribute("menu", "ppm");
            request.setAttribute("submenu", "penawaranDosen");

            RequestDispatcher dis = request.getRequestDispatcher("ppm/halamanDetailPenawaranPpmDosen.jsp");
            dis.forward(request, response);

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
